package com.bosssoft.trainee.nontax.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bosssoft.trainee.nontax.dao.UserMapper;
import com.bosssoft.trainee.nontax.entity.dto.DepartmentDTO;
import com.bosssoft.trainee.nontax.entity.po.DepartmentPO;
import com.bosssoft.trainee.nontax.dao.DepartmentMapper;
import com.bosssoft.trainee.nontax.entity.po.UserPO;
import com.bosssoft.trainee.nontax.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.trainee.nontax.service.HystrixService;
import com.bosssoft.trainee.nontax.utils.DepartmentTree;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sct
 * @since 2022-06-15
 */
@Service
@Transactional
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, DepartmentPO> implements DepartmentService , HystrixService {

    @Resource
    private UserMapper userMapper;

    /**
     * 查询部门列表
     * @param
     * @return
     */
    @Override
    @HystrixCommand(fallbackMethod = "deptInfo_Ok",
            commandProperties =
                    //规定 5 秒钟以内就不报错，正常运行，超过 5 秒就报错，调用指定的方法
                    {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")})
    public List<DepartmentDTO> findDepartmentList(DepartmentDTO departmentDTO) {
        // 创建条件构造器对象
        QueryWrapper<DepartmentPO> queryWrapper =new QueryWrapper<>();
        // 部门名称
        queryWrapper.like(!ObjectUtils.isEmpty(departmentDTO.getDepartmentName()),"department_name",departmentDTO.getDepartmentName());
        queryWrapper.orderByAsc("order_num");
        List<DepartmentPO> departmentPOList = baseMapper.selectList(queryWrapper);
        departmentPOList=DepartmentTree.makeDepartmentTree(departmentPOList, 0L);

        List<DepartmentDTO> departmentDTOList;
        departmentDTOList = JSON.parseArray(JSON.toJSONString(departmentPOList), DepartmentDTO.class);
        return departmentDTOList;
    }

    /**
     * 查询上级部门列表
     * @return
     */
    @Override
    public List<DepartmentDTO> findParentDepartment() {
        //创建条件构造器对象
        QueryWrapper<DepartmentPO> queryWrapper = new QueryWrapper<>();
        //排序
        queryWrapper.orderByAsc("order_num");
        //查询部门列表
        List<DepartmentPO> departmentPOList = baseMapper.selectList(queryWrapper);
        //创建部门对象
        DepartmentPO departmentPO = new DepartmentPO();
        departmentPO.setId(0L);
        departmentPO.setDepartmentName("顶级部门");
        departmentPO.setPid(-1L);
        departmentPOList.add(departmentPO);
        // 生成部门树列表
        departmentPOList=DepartmentTree.makeDepartmentTree(departmentPOList,-1L);
        List<DepartmentDTO> departmentDTOList;
        departmentDTOList = JSON.parseArray(JSON.toJSONString(departmentPOList), DepartmentDTO.class);
        return departmentDTOList;
    }

    /**
     * 判断部门下是否有子部门
     * @param id
     * @return
     */
    @Override
    public boolean hasChildrenOfDepartment(Long id) {
        QueryWrapper<DepartmentPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",id);
        Long selectCount = baseMapper.selectCount(queryWrapper);
        return selectCount> 0;
    }

    /**
     * 判断部门下是否存在用户
     * @param id
     * @return
     */
    @Override
    public boolean hasUserOfDepartment(Long id) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id",id);
        return userMapper.selectCount(queryWrapper) > 0;
    }


    @Override
    public List<DepartmentDTO> deptInfo_Ok(DepartmentDTO departmentDTO) {
        return null;
    }
}
